(And if you have created some nice things, think about sharing them, too!)
This is a Plugin for REALbasic. Plugins contain usually low-level code that cannot be written using the REALbasic language directly.
The plugin and the sample RB program were created and tested using CodeWarrior Pro 3 and REALbasic versions 1.1.1 and 2.0.2.
More REALbasic examples can be found on my Web Site at
<http://www.tempel.org/rb>
Enjoy!
Overview
This plugin gives you access to some functions of the Mac OS File Manager, such as relating RB's FolderItems to a Volume Reference Number (VRefNum), checking for Server volumes and getting more information about files (File Flags and Attributes).
One of the functions is particularly helpful: RB 1.1.1 (and before) crashes when you open and then again close the resource fork of a file that is already opened by another application or by the RB IDE. To prevent that, you can use IsResourceForkOpen before you open the resource fork and then do not close the resource fork again if it was open before.
Installation
1. Quit REALbasic if it is running
2. Place the file TT's FileMgr-Plugin into a folder called Plugins, that resides in the same folder where your REALbasic application is.
3. Now you can launch REALbasic again and the methods described below will be available for your RB applications and the enclosed demo code.
Plugin Functions
• NewFolderItemFromFSSpec(fsSpec as MemoryBlock) as FolderItem
Creates a FolderItem from a FSSpec record, which must be passed in as a String of exactly 70 bytes in length (which means that LenB(fsSpec) must be equal to 70).
• NewFolderItem(vRefNum as Integer, parID as Integer, name as String) as FolderItem
Creates a FolderItem from a specified volume, the item's directory ID and its name.
• IsOnRemoteVolume(fileOrFolder as FolderItem) as Boolean
Returns true if the file or folder is located on a server volume.
• VolGetVRefNum(fileOrFolder as FolderItem) as Integer
Returns the volume reference number (vRefNum) of any file or folder. Those vRefNums are unique to a mounted volume. For example, to find out if two files are on the same volume, check if their vRefNums are equal.
• VolumeByVRefNum(vRefNum as Integer) as FolderItem
Pass in the vRefNum of any mounted volume and it returns the Volume as a FolderItem.
Returns nil if the vRefNum was invalid.
• GetFileFlags(file as FolderItem) as Integer
Returns the fdFlags of a file. If you pass in a non-existing file or a folder, a negative error code is returned instead.
The fdFlags is a set of bits with the following meaning:
• bit 15, value &H8000: isAlias
• bit 14, value &H4000: isInvisible
• bit 13, value &H2000: hasBundle (has a BNDL resource)
• bit 12, value &H1000: nameLocked
• bit 11, value &H0800: isStationary
• bit 10, value &H0400: hasCustomIcon
• bit 8, value &H0100: hasBeenInited (Finder has seen the file since it has been created)
• bit 7, value &H0080: hasNoINITs (there's no INIT rsrc in the Extension file)
• bit 6, value &H0040: isShared
• bits 1-3, value &H000E: color (as a 3-bit value from 0-7)
For example, testing for a file being invible works like this:
• SetFileFlags(file as FolderItem, flags as Integer) as Integer
Sets the fdFlags of a file. Returns an error code (or zero if no error occured). Possible error conditions include "disk is write protected" and "file not found".
When changing flags of a file, use GetFileFlags to get the original flags, then clear or set the flags by using BitwiseAnd and BitwiseOr and call SetFileFlags to set the new flags.
For example, clearing a file's hasCustomIcon flag works like this:
fdFlags = GetFileFlags(f)
if fdFlags >= 0 then
err = GetFileFlags(f, BitSet(fdFlags, 10, false))
if err <> 0 then
... oops, an error occured (for instance, the disk could be write protected)
end
end
• GetFolderFlags(folder as FolderItem) as Integer
Returns the frFlags of a folder. If you pass in a non-existing folder, a negative error code is returned instead.
The frFlags are similar to the fdFlags (see GetFileFlags), but only a subset of them is used with folders.
• SetFolderFlags(folder as FolderItem, flags as Integer) as Integer
Sets the frFlags of a folder. Returns an error code (or zero if no error occured). Possible error conditions include "disk is write protected" and "folder not found".
When changing flags of a folder, use GetFolderFlags to get the original flags, then clear or set the flags by using BitwiseAnd and BitwiseOr and call SetFolderFlags to set the new flags.
• GetFileAttribute(file as FolderItem) as Integer
Returns the flAttrib of a file. If you pass in a non-existing file or a folder, a negative error code is returned instead.
The flAttrib is a set of bits with the following meaning:
• bit 0, value 1: file is locked
• bit 2, value 4: resource fork is open
• bit 3, value 8: data fork is open
• bit 4, value 16: item is a directory
• bit 7, value 128: file (one or both forks) is open
(See GetFileFlags for an example on how to check the bits)
• IsFileOpen(file as FolderItem) as Boolean
Returns true if the item is a file and that file is open (in use). Equivalent to calling GetFileAttribute and testing bit 7.
• IsResourceForkOpen(file as FolderItem) as Boolean
Returns true if the item is a file and that its resource fork is open (in use). Equivalent to calling GetFileAttribute and testing bit 2.
• VolGetFolderItemID(item as FolderItem, createFileIDs as Boolean) as Integer
Returns the unique FileID or DirID of a FolderItem. This ID is unique over all items on the same volume. The value 2 always identifies the root directory, all negative values and positive ones above 15 are used for user-created files and folders, while the values 3-15 are used internally by the File System (for the Desktop Database, for example).
If the item does not exists, 0 (zero) is returned instead.
IDs for Folders can always be resolved back to a FolderItem using VolResolveID, while resolving FileIDs only works when they've previously been created explicitly. To create a resolvable FileID, pass true to the createFileIDs parameter. But be aware that if the FileID can not be created (because the disk is locked or because the File System Format does not support it), the call will fail and a zero will be returned!
So, if you are just interested in reading the FileID, pass false to the second parameter. This will not fail even there hasn't been created a resolvable ID for that file yet.
• VolResolveID(vRefNum as Integer, id as Integer) as FolderItem
• VolResolveID(vol as FolderItem, id as Integer) as FolderItem
Both functions return a FolderItem for a passed FileID or DirID. If the item can not be resolved, nil is returned instead.
The first parameter specified the volume where you want to resolve the ID on.
Programming Information
The plugin code is a Metrowerks CodeWarrior Pro 3 (IDE 3.1) project.
If you have an older CW version that refuses to open the prj file, then create a new and set/enable the following prj options:
• create 68K target (set to 68K linker)
• prj type: Code Resource, File Name "CatalogSearch-Plugin", Creator "SfTg", Type "RBPl", ResType "PL68", ResID "128", Extended Resource, Header Type "Standard"
• C/C++ language settings: Prefix File "MacHeaders.h"
• 68K Processor: 68020 Codegen, Code Model "Smart", Struct Alignm. "PowerPC", 4-Byte Ints, 8-Byte Doubles, MPW C Call. Conv.
• 68K Linker: Link Single Segment, Merge Compiler Glue…, Dead-strip Static Init Code
• Add the files "Plugin Source.cpp", "PluginMain.cpp", "MacOS.lib", "Version Info.rsrc" and a 68K C lib with "(4i_8d)" and "A4" in its name to the project.
To compile the project, you also need to have the "Plugin SDK" available, which you can download from RB's website.
Revision history
8 Mar 99, v1.0:
First release.
26 March 99, v1.1
Added the functions SetFileFlags, Set/GetFolderFlags, VolGetFolderItemID and VolResolveID.
Added the module BitwiseTools to the sample project that provides functions to check and set the bits of an Integer.
4 July 99, v1.2
Added two routines to create a FolderItem from Mac OS file/folder specs (NewFolderItem...).